// ###############################################
//
// AUTHOR: Nick LaPrell (http://nick.laprell.org)  
//
// Expands text fields when user input exceeds
// the width of the field.
//
// JQuery.expandField()
//   Prepares the field(s) to expand when user input
//   exceeds its width.
//
// ###############################################

jqPlugins.expandField = true;

(function($){

  // KICK OFF AUTOMATICALLY FOR FIELDS WITH A CLASS OF "expandable"
  if(typeof init != 'undefined'){init.expandField = true;}

  $.fn.expandField = function() {  
    var widen = function(){
      if(typeof fieldWidths == 'undefined'){
        fieldWidths = new Object();
      }
      if(fieldWidths[$(this).attr("name")] === undefined){
        fieldWidths[$(this).attr("name")] = $(this).css("width").replace("px","")*1+4+"px";
      }
      var container = document.createElement('span');
      container.style.fontSize = $(this).css("font-size");
      document.body.appendChild(container);
      container.innerHTML = $(this).val();
      var width = container.offsetWidth+4;
      document.body.removeChild(container);
      if(width >= $(this).width()){
        $(this).css({position:"absolute", width:width + "px", zIndex:"100"});
      }
    }
    var restore = function(){
      $(this).css({position:"", width:fieldWidths[$(this).attr("name")], zIndex:""});
    }
    return this.each(function() {
      $(this).bind("keyup.widen", widen);
      $(this).bind("focus.widen", widen);
      $(this).bind("blur.widen", restore); 
    });  
  }; 
})(jQuery);